Commit 09222b7
feat(core,egg): add V8 startup snapshot lifecycle hooks and APIs (#5856)
## Summary
Add V8 startup snapshot support to the egg framework package:
- **Metadata-only loading**: `snapshot: true` option triggers lifecycle
cutoff after `configWillLoad` (via PR2 core), then awaits `loadFinished`
for loader completion
- **`buildSnapshot()` / `restoreSnapshot()` APIs**: Public entry points
for snapshot construction and restoration
- **`startEggForSnapshot()`**: Creates Agent + Application in
single/snapshot mode, awaits full loader completion via `loadFinished`
promise
- **`registerSnapshotCallbacks()`**: Serialize/deserialize hooks for
loggers, messenger, agent keep-alive timer, unhandledRejection handler,
HttpClient prototype
- **Snapshot guards**: Skip runtime-only init (timers, event listeners,
config dumping) when `snapshot: true`
This is **PR4 of 6** in the V8 startup snapshot series. Depends on PR1
(koa), PR2 (core), PR3 (utils).
## Changes (7 files, +294/-47)
- `packages/egg/src/lib/egg.ts` — Snapshot option, loadFinished promise,
registerSnapshotCallbacks(), guards
- `packages/egg/src/lib/agent.ts` — Conditional keepalive,
registerSnapshotCallbacks() override
- `packages/egg/src/lib/application.ts` — Skip bindEvents in snapshot
mode
- `packages/egg/src/lib/snapshot.ts` — NEW:
buildSnapshot/restoreSnapshot APIs
- `packages/egg/src/lib/start.ts` — NEW: startEggForSnapshot()
- `packages/egg/src/index.ts` — Export snapshot utilities
- `packages/egg/test/lib/snapshot.test.ts` — NEW: 18 comprehensive tests
## Test plan
- [x] 18 snapshot-specific tests (buildSnapshot, restoreSnapshot,
startEggForSnapshot, registerSnapshotCallbacks, loadFinished, guards)
- [x] All existing egg tests pass (54 files)
- [x] oxlint --type-aware clean
- [x] /simplify reviewed
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **New Features**
* V8 startup snapshot support with build/restore utilities and a
convenience snapshot startup flow.
* Lifecycle hooks and app/agent resume behavior for
serialize/deserialize, plus public entry points to trigger them.
* Improved startup semantics: app/agent expose a load-completion promise
and agent keep‑alive is started on demand.
* **Tests**
* Comprehensive snapshot-mode tests covering hook ordering, resource
cleanup/restore, startup/resume sequencing, and restore errors.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>1 parent 7f7d4f4 commit 09222b7
10 files changed
Lines changed: 1028 additions & 29 deletions
File tree
- packages
- core
- src
- test
- egg
- src
- lib
- test
- __snapshots__
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
412 | 412 | | |
413 | 413 | | |
414 | 414 | | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
415 | 433 | | |
416 | 434 | | |
417 | 435 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
64 | 81 | | |
65 | 82 | | |
66 | 83 | | |
| |||
89 | 106 | | |
90 | 107 | | |
91 | 108 | | |
| 109 | + | |
92 | 110 | | |
93 | 111 | | |
94 | 112 | | |
| |||
105 | 123 | | |
106 | 124 | | |
107 | 125 | | |
| 126 | + | |
108 | 127 | | |
109 | 128 | | |
110 | 129 | | |
| |||
264 | 283 | | |
265 | 284 | | |
266 | 285 | | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
267 | 289 | | |
268 | | - | |
| 290 | + | |
| 291 | + | |
269 | 292 | | |
270 | 293 | | |
271 | 294 | | |
| |||
380 | 403 | | |
381 | 404 | | |
382 | 405 | | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
383 | 488 | | |
384 | 489 | | |
385 | 490 | | |
| |||
389 | 494 | | |
390 | 495 | | |
391 | 496 | | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
392 | 500 | | |
393 | 501 | | |
394 | 502 | | |
| |||
0 commit comments