Skip to content

feat(service-worker): standalone service worker runtime#6022

Open
gxkl wants to merge 67 commits into
nextfrom
feat/service-worker
Open

feat(service-worker): standalone service worker runtime#6022
gxkl wants to merge 67 commits into
nextfrom
feat/service-worker

Conversation

@gxkl

@gxkl gxkl commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds a standalone service-worker host for running tegg HTTP controllers and MCP tools without starting an Egg application. The same controller metadata and registration core now serve both the Egg node-HTTP host and Fetch-compatible runtimes, with a runnable Node/Cloudflare example.

Architecture

  • Extracts @eggjs/controller-runtime as the host-neutral controller library. Egg transport remains in @eggjs/controller-plugin; Fetch transport lives in @eggjs/service-worker-controller; @eggjs/service-worker is the host facade over StandaloneApp.
  • HTTP and MCP registration use the same collect-then-finalize model. Egg finalizes both from @LoadUnitInstanceLifecycleProto providers when CONTROLLER_LOAD_UNIT is created; the Fetch host finalizes once while building routes on the first event.
  • Controller middleware supports dependency-injected Advice through the existing @Middleware(AdviceClass) form. The controller runtime invokes around() at the bound HTTP/MCP invocation boundary and preserves AbstractControllerAdvice.middleware() compatibility, while explicit @Pointcut remains owned by the independent AOP plugin.
  • Framework extension hooks are declarative and per app: controller graph weaving is registered by a scanned inner object, and mcp-proxy injects the current EggMcpRouter instead of writing to a static hook registry.

Standalone runtime

  • Supports Node serve(), embedded handleEvent(), and Cloudflare module-worker entries.
  • Supports Fetch Request/Response, parameter binding, binary and streaming bodies, multiple Set-Cookie headers, request-scope stream draining, host-provided error mapping, and background tasks.
  • Exposes MCP stateless Streamable HTTP with fresh SDK server/transport instances per request, controller and method middleware, authentication, DNS-rebinding options, and host-registered transport providers.
  • Keeps mutable runtime state isolated through each app's TeggScope bag.

Bundle and module loading

  • Promotes TeggManifest to the shared tegg type surface and uses the shared ManifestLoaderFS overlay in both Egg and standalone hosts.
  • ModuleLoader.createModuleLoader() retains the selected file view in the current app scope, so later dynamic loaders such as DAL discovery read manifest-indexed files in a bundle without expanding multi-instance callback context.
  • Adds standalone support to egg-bin bundle, including TypeScript metadata scanning in a loader-enabled child process and Cloudflare-compatible worker output.
  • Keeps the example minimal: main.ts is the Node entry and worker.ts is the Cloudflare entry.

Validation

  • Focused controller graph, mcp-proxy registration, standalone HTTP, and standalone MCP suites: 46 tests.
  • Typechecked controller-runtime, controller-plugin, mcp-proxy-plugin, and service-worker-controller.
  • The current pushed revision is green across CI on Linux/macOS/Windows with Node 22/24, CodeQL, examples/cnpmcore E2E, typecheck, and package checks.

Documentation

Updates the service-worker, controller Advice, module-plugin lifecycle, LoaderFS/bundler, local CI, package README, and minimal-example documentation to match the final runtime contracts.

@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Adds a host-agnostic controller runtime, refactors Egg controller registration, and introduces standalone service-worker runtime and HTTP/MCP transports. It also adds ServiceWorkerApp, integration tests, examples, package metadata, and documentation.

Changes

Standalone service-worker controller

Layer / File(s) Summary
Controller runtime contracts and registration
tegg/core/controller-runtime/...
Adds host-agnostic load-unit, metadata, HTTP, and MCP registration contracts, including route conflict detection and schema-loading hooks.
Egg controller integration and compatibility loading
tegg/plugin/controller/..., tegg/plugin/tegg/..., tegg/core/runtime/...
Moves Egg HTTP/MCP wiring into lifecycle registrars and providers, updates MCP hook ownership, and changes app-compatible prototype loading.
Standalone service-worker runtime and transports
tegg/standalone/service-worker*/..., tegg/core/standalone-decorator/...
Adds event dispatch, fetch contexts, HTTP routing, stateless MCP routing, authentication, response utilities, and streaming lifecycle handling.
Validation, examples, packaging, and documentation
tegg/standalone/service-worker/test/..., examples/helloworld-service-worker/..., wiki/...
Adds integration coverage, runnable examples, package/build metadata, module-plugin documentation, and stale-cache guidance.

Estimated code review effort: 5 (Critical) | ~120 minutes

Suggested reviewers: killagu, copilot

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: adding a standalone service-worker runtime.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/service-worker

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 6, 2026

Copy link
Copy Markdown

Deploying egg with  Cloudflare Pages  Cloudflare Pages

Latest commit: 3dc0448
Status: ✅  Deploy successful!
Preview URL: https://6247909c.egg-cci.pages.dev
Branch Preview URL: https://feat-service-worker.egg-cci.pages.dev

View logs

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

Copy link
Copy Markdown
Contributor

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 introduces standalone service worker support for @eggjs/tegg modules, allowing HTTP controllers and MCP tools to be served over a fetch event loop without requiring a full Egg application. It extracts the host-agnostic controller registration runtime into @eggjs/controller-runtime and adds the @eggjs/service-worker-runtime and @eggjs/service-worker packages, along with a runnable example. The review feedback suggests improving exception handling in FetchEventHandler by checking if caught errors are instances of Error before accessing their message, and expanding ResponseUtils to correctly handle and pass through standard web-standard BodyInit types (such as Uint8Array and Blob) instead of incorrectly serializing them as JSON.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread tegg/standalone/service-worker-controller/src/http/FetchEventHandler.ts Outdated
Comment thread tegg/standalone/service-worker-controller/src/utils/ResponseUtils.ts Outdated
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 6, 2026

Copy link
Copy Markdown

Deploying egg-v3 with  Cloudflare Pages  Cloudflare Pages

Latest commit: 3dc0448
Status: ✅  Deploy successful!
Preview URL: https://35abf8d4.egg-v3.pages.dev
Branch Preview URL: https://feat-service-worker.egg-v3.pages.dev

View logs

@gxkl
gxkl force-pushed the feat/service-worker branch from 544b347 to 501a495 Compare July 6, 2026 00:25
@socket-security

socket-security Bot commented Jul 6, 2026

Copy link
Copy Markdown

Dependency limit exceeded — report not shown.

This pull request scan exceeded the 10,000-dependency limit applied to this scan, so the results are incomplete and may be inaccurate. To avoid reporting false positives, Socket has not posted a report.

Upgrade your plan to raise the dependency limit and get complete reports, or view the partial scan in the dashboard.

Socket is always free for open source. If this is a non-commercial open source project, contact us to request a free Team account.

@gxkl
gxkl force-pushed the feat/module-plugin-core branch from 5001fb6 to 79594a6 Compare July 6, 2026 01:39
@gxkl
gxkl force-pushed the feat/service-worker branch 4 times, most recently from bcb170d to 79d6bc3 Compare July 6, 2026 10:07
@gxkl
gxkl force-pushed the feat/module-plugin-core branch 8 times, most recently from b774e07 to 5c42a6d Compare July 9, 2026 15:46
@gxkl
gxkl force-pushed the feat/service-worker branch from 79d6bc3 to 27cb9ef Compare July 10, 2026 05:18
@gxkl
gxkl force-pushed the feat/module-plugin-core branch 9 times, most recently from 85a0476 to ad9acf9 Compare July 13, 2026 08:17
@gxkl
gxkl force-pushed the feat/service-worker branch from 9434e0b to 078a2d9 Compare July 13, 2026 08:24
Copilot AI review requested due to automatic review settings July 20, 2026 05:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings July 20, 2026 08:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings July 20, 2026 08:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings July 20, 2026 16:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings July 21, 2026 10:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings July 21, 2026 10:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings July 21, 2026 10:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings July 21, 2026 10:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@gxkl
gxkl requested a review from killagu July 21, 2026 10:59
Copilot AI review requested due to automatic review settings July 21, 2026 11:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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.

4 participants