Skip to content

Architecture overview

lastcall is a zero-dependency TypeScript library with a small internal architecture:

createLastcall()
    ├── HandlerRegistry     — stores handlers, priority, deps, phases
    ├── SignalListener      — SIGTERM / SIGINT / SIGHUP
    ├── LastcallEvents      — beforeShutdown, afterShutdown, handler events
    └── shutdown-runner     — phase execution, timeouts, HTTP drain

Singleton

getDefaultLastcall() returns a process-wide singleton via globalThis — safe when the module is imported multiple times.

State machine

idle → shutting_down → [phases] → done | force_exit

During shutdown, subsequent signals are ignored (idempotent).

Module map

FileResponsibility
create-lastcall.tsPublic factory, options, orchestration
handler-registry.tsRegister handlers, topo sort, batches
shutdown-runner.tsRun phases, timeouts, HTTP handler
signal-listener.tsOS signal registration
events.tsTyped event emitter
utils/topological-sort.tsDependency ordering
utils/singleton.tsGlobal singleton
utils/logger.tsDefault [lastcall] logger

Design principles

  1. Zero production dependencies
  2. Fail-safe — listener errors never break shutdown
  3. Explicit phases — drain before cleanup
  4. Escape hatches — custom exit, logger, autoExit: false

See Shutdown lifecycle for the full flow.