Skip to content

Commit ade8dd9

Browse files
gxklclaude
andcommitted
docs(wiki): service worker package page
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 1ff8952 commit ade8dd9

3 files changed

Lines changed: 60 additions & 0 deletions

File tree

wiki/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Read this file before exploring raw sources.
2727
- [Egg Bundler](./packages/egg-bundler.md) - Tooling package that bundles Egg applications and backs `egg-bin bundle`.
2828
- [Loader FS Package](./packages/loader-fs.md) - Shared loader-facing filesystem boundary for Egg loaders and future bundled runtimes.
2929
- [Onerror Plugin](./packages/onerror.md) - Default Egg error-handling plugin and configurable response negotiation layer.
30+
- [Standalone Service Worker](./packages/service-worker.md) - Fetch-semantics standalone runtime serving HTTP controllers and MCP tools from a tegg module without an egg application.
3031
- [Typings Package](./packages/typings.md) - Shared TypeScript type surface for cross-package Egg typings.
3132
- [Utils Package](./packages/utils.md) - Shared utility package for module loading and bundled module-loader integration.
3233

wiki/log.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,9 @@ Full **isolate:false suite validated GREEN** under CI-faithful parallelism (`--m
132132
- sources touched: `tegg/plugin/aop/src/lib/AopContextHook.ts`, `tegg/core/aop-runtime/src/AopContextAdviceRegistry.ts`, `tegg/core/aop-runtime/src/LoadUnitAopHook.ts`, `tegg/plugin/dal/src/index.ts`, `tegg/plugin/dal/src/lib/DalModuleLoadUnitHook.ts`
133133
- pages updated: `wiki/log.md`, `wiki/concepts/tegg-module-plugin.md`
134134
- note: Replaced stale DAL source paths and updated the AOP note after `AopContextHook` moved to lifecycle-proto/inner-object registration backed by `AopContextAdviceRegistry`.
135+
136+
## [2026-07-05] package | standalone service worker (方案二 complete)
137+
138+
- sources touched: `tegg/plugin/controller`, `tegg/standalone/{service-worker-runtime,service-worker}`, `examples/helloworld-service-worker`
139+
- pages updated: `wiki/index.md`, `wiki/log.md`, `wiki/packages/service-worker.md`
140+
- note: Completed the service-worker migration on top of the module plugin mechanism: made the controller plugin a dual-host module carrying its host-agnostic runtime under `lib/runtime/`, added the two service worker packages (fetch adapter + protocol-agnostic runtime), MCP stateless streamable HTTP via the SDK's web-standard transport (SDK >= 1.29 forbids stateless transport reuse — fresh server+transport per request), streaming-response lifecycle via BackgroundTaskHelper drain, unified `{ code, message }` errors, `mcpAuthHandler` auth extension point, and a runnable example. Gotcha recorded: frameworkDeps module scans must exclude `test/**` or framework test fixtures load as business modules.

wiki/packages/service-worker.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
title: Standalone service worker (@eggjs/service-worker[-runtime])
3+
type: package
4+
summary: Fetch-semantics standalone runtime — HTTP controllers and MCP tools served from a tegg module without an egg application
5+
source_files:
6+
- tegg/standalone/service-worker-runtime/src
7+
- tegg/standalone/service-worker/src
8+
- examples/helloworld-service-worker
9+
updated_at: 2026-07-05
10+
status: active
11+
---
12+
13+
Two packages provide the standalone service worker runtime on top of the
14+
module-plugin mechanism (declarative `@InnerObjectProto` /
15+
`@XxxLifecycleProto` hooks, see the module-plugin pages):
16+
17+
- `@eggjs/service-worker-runtime` — protocol-agnostic: `@Runner()` entry
18+
dispatching events to `@EventHandlerProto('<type>')` handlers, event
19+
injection into ContextProtos, `BackgroundTaskHelper` re-export
20+
(ctx-destroy draining).
21+
- `@eggjs/service-worker` — the fetch protocol: `FetchEventHandler`,
22+
`FetchRouter` + fetch parameter binding (no `@Cookies`), MCP stateless
23+
streamable HTTP under `/mcp[/name]/stream`, `ServiceWorkerApp` facade with
24+
`serve()` (node:http bridge) and embedded `handleEvent()`.
25+
26+
Key mechanics and constraints:
27+
28+
- **Controller reuse**: metadata from `@eggjs/controller-decorator`,
29+
registration runtime from `@eggjs/controller-plugin` (shared with
30+
`@eggjs/controller-plugin`); only param binding and transports are
31+
fetch-specific.
32+
- **MCP SDK >= 1.29 stateless transports are single-shot** (reuse throws), so
33+
each MCP request builds a fresh `MCPServerHelper` + web-standard transport
34+
from register records collected at boot. The service worker uses
35+
`WebStandardStreamableHTTPServerTransport` (Request in, Response out) — no
36+
node req/res bridging. Auth is an `mcpAuthHandler` extension point
37+
(default: allow).
38+
- **Streaming lifecycle**: `FetchEventHandler` routes every response body
39+
through a passthrough and registers the drain as a background task, so ctx
40+
destroy waits (bounded by `config.backgroundTask.timeout`) until the client
41+
consumes the stream.
42+
- **Unified errors**: framework failures reply `{ code, message }` JSON
43+
(`NOT_FOUND` / `INTERNAL_SERVER_ERROR`); `ctx.responseHeaders` merge onto
44+
the final response.
45+
- **frameworkDeps scan excludes `test/**`\*\*: the framework packages are
46+
themselves modules; without the exclusion their test fixtures load as
47+
business modules (duplicate controller names) in workspace layouts.
48+
- Per-app state is all inner objects in the app's TeggScope bag — two
49+
concurrent `ServiceWorkerApp`s are isolated (`test/MultiApp.test.ts`).
50+
51+
Example: `examples/helloworld-service-worker` (entry `main.ts` lives outside
52+
the scanned `app/` module dir on purpose — the scan imports every module
53+
file, and importing an entry that boots the app recurses).

0 commit comments

Comments
 (0)