Files
suaveplan/packages/api-stack/api/api-router-in-memory/README.md
2026-05-27 23:29:53 +01:00

75 lines
5.1 KiB
Markdown

# @suaveplan/api-router-in-memory — in-memory router adapter for the @suaveplan/api plugin system. Implements both the abstract Router contract from @suaveplan/api-router AND the PipelineRouter contract from @suaveplan/api-router on a single InMemoryRouter class. Production-grade in-memory adapter — keeps state in process, dispatches without binding a socket. Passes runRouterConformance + runPipelineRouterConformance.
Stage-5 in-memory router adapter for `@suaveplan/api`. Implements both the abstract `Router` contract from `@suaveplan/api-router` AND the `PipelineRouter` contract from `@suaveplan/api-router` on a single `InMemoryRouter` class. Production-grade in-memory adapter — not a test double.
<!-- begin:auto-docs -->
## Documentation tree
- [adapter](./src/adapter/adapter.md) — `InMemoryRouter` is the Stage-5 in-memory router adapter for `@suaveplan/api`. It implements both…
- [adapter](./src/adapter/adapter.md) — `InMemoryRouter` is the Stage-5 in-memory router adapter for `@suaveplan/api`. It implements both…
- [blue-green-slot](./src/blue-green-slot/blue-green-slot.md) — `InMemoryBlueGreenSlot` is the `BlueGreenSlot` implementation paired with `InMemoryRouter`. It is a thin extension of `…
- [blue-green-slot](./src/blue-green-slot/blue-green-slot.md) — `InMemoryBlueGreenSlot` is the `BlueGreenSlot` implementation paired with `InMemoryRouter`. It is a thin extension of `…
- [error-codes](./src/error-codes/error-codes.md) — Defines the stable `ROUTER_IN_MEMORY.*` error-code namespace used by every production path in `@suaveplan/api-router-in…
- [error-codes](./src/error-codes/error-codes.md) — Defines the stable `ROUTER_IN_MEMORY.*` error-code namespace used by every production path in `@suaveplan/api-router-in…
- [pipeline](./src/pipeline/pipeline.md) — `PipelineRouteTable` is the in-memory metadata store that backs `InMemoryRouter`'s pipeline facet. It stores per-route…
- [pipeline](./src/pipeline/pipeline.md) — `PipelineRouteTable` is the in-memory metadata store that backs `InMemoryRouter`'s pipeline facet. It stores per-route…
- [testing](./src/testing/testing.md) — `createInMemoryRouterHarness()` is the canonical "spin up an in-memory router for a test" entry point. It bundles a fre…
- [testing](./src/testing/testing.md) — `createInMemoryRouterHarness()` is the canonical "spin up an in-memory router for a test" entry point. It bundles a fre…
<!-- end:auto-docs -->
## What it ships
- `InMemoryRouter` / `createInMemoryRouter()` — concrete `Router` adapter that subclasses the abstract base AND exposes the pipeline facet (`registerPipelineRoute`, `matchPipeline`, `listPipelineRoutes`, `unregisterPipelineRoute`, `unregisterPipelinePlugin`).
- `InMemoryBlueGreenSlot` / `createInMemoryBlueGreenSlot` — hot-reload primitive paired with the router. Thin extension of `@suaveplan/api-router`'s reference `InMemoryBlueGreenSlot`.
- `PipelineRouteTable` — the in-memory metadata store backing the pipeline facet, exported for advanced use cases that need to share a table across routers.
- `createInMemoryRouterHarness()` — downstream test helper that bundles a router + slot factory pair, replacing the hand-rolled router shims that have grown across the foundation + plugin tests.
- `ROUTER_IN_MEMORY_CODES` / `RouterInMemoryError` — stable error-code registry (`ROUTER_IN_MEMORY.ROUTE_COLLISION`, `ROUTER_IN_MEMORY.INVALID_PREFIX`).
## Conformance
The package passes BOTH foundation conformance suites:
- `runRouterConformance` from `@suaveplan/api-router/testing` — see `tests/conformance/api-router.test.ts`.
- `runPipelineRouterConformance` from `@suaveplan/api-router/testing/pipeline-router-conformance` — see `tests/conformance/pipeline-router.test.ts`.
Adding new behaviours requires re-running both harnesses.
## Modules
- [`src/adapter/adapter.md`](./src/adapter/adapter.md) — `InMemoryRouter` + factory.
- [`src/blue-green-slot/blue-green-slot.md`](./src/blue-green-slot/blue-green-slot.md) — hot-reload primitive.
- [`src/pipeline/pipeline.md`](./src/pipeline/pipeline.md) — `PipelineRouteTable` + types.
- [`src/testing/testing.md`](./src/testing/testing.md) — `createInMemoryRouterHarness` for downstream tests.
- [`src/error-codes/error-codes.md`](./src/error-codes/error-codes.md) — `ROUTER_IN_MEMORY.*` registry.
## Usage
```ts
import {
createInMemoryRouter,
InMemoryBlueGreenSlot,
} from "@suaveplan/api-router-in-memory";
const router = createInMemoryRouter({ plugin: "billing" });
router.get("/invoices/:id", () => null);
router.registerPipelineRoute({
plugin: "billing",
method: "POST",
path: "/invoices",
handler: async () => ({ ok: true }),
});
const slot = new InMemoryBlueGreenSlot("billing", () => "v1");
slot.setShadow(() => "v2");
const draining = slot.flip();
await slot.dispose(draining.slotId, { timeoutMs: 100 });
```
## When to use
- Foundation tests that need a real `Router`/`PipelineRouter` without a Hono peer.
- Plugin tests that previously hand-rolled their own router shim (`api-router-hono` carries the Hono peer-dep cost).
- The default `router: "in-memory"` slot in the Stage-5 `@suaveplan/api-host/testing` adapter table.