feat(routing): add OSRM as an alternate routing backend behind a provider abstraction#191
feat(routing): add OSRM as an alternate routing backend behind a provider abstraction#191jasoneplumb wants to merge 1 commit into
Conversation
…ider abstraction (#189) Refactors src/routing.ts into a provider-agnostic dispatch layer with two backends behind it: the existing Valhalla client and a new OSRM client targeting the OSM-DE public backend at routing.openstreetmap.de. The active provider is chosen by a top-level const (ROUTING_PROVIDER), still defaulting to Valhalla — runtime failover is left to a follow-up issue. Both providers produce the same Route / RouteStep shape so guidance.ts and the pill UI are unchanged. OSRM maneuvers are translated into Valhalla-compatible numeric type codes (so the existing icon picker keeps working) and into short English instruction strings synthesized from maneuver.type + modifier + step.name. Closes #189 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Code Review — feat(routing): OSRM backend + provider abstractionOverall this is a well-structured refactor. The extraction is clean, tests are thorough, and the normalisation layer that maps OSRM maneuvers onto Valhalla type codes is a smart move to keep High: Circular import
Vite/esbuild typically resolves these correctly at build time, but it's brittle — tree-shaking and init order can produce subtle runtime surprises. The clean fix is a small High: Bundle size vs. documented capCLAUDE.md documents the Medium:
|
Summary
src/routing.tsinto a provider-agnostic dispatch layer; the active backend is selected by a top-levelROUTING_PROVIDERconst (defaults tovalhalla).routing.openstreetmap.de) so we have a swappable peer when FOSSGIS Valhalla is unavailable.Route/RouteStepshape Valhalla produces —guidance.tsand the pill UI are unchanged.Changes
src/routing.ts— Public types (Costing,Route,RouteStep,RouteRequest),decodePolyline6, andfetchRoute()dispatch viaROUTING_PROVIDER. Re-exportsVALHALLA_URLandOSRM_BASE_URL.src/routing-valhalla.ts(new) — Existing Valhalla client extracted intact (POST /route, JSON body, prose instruction strings).src/routing-osrm.ts(new) — OSRM client. Selects the correct OSM-DE sub-backend (routed-car/routed-bike/routed-foot) perCostingand requestsoverview=full&geometries=polyline6&steps=true. Builds the unifiedcoords[]by concatenating per-step geometries with shared-endpoint dedup, sobeginShapeIndexlines up with the existing guidance state machine.src/routing-osrm-instructions.ts(new) — Translates OSRMmaneuver.type+modifierto Valhalla-compatible numeric type codes (soguidance.ts's icon picker keeps working) and synthesises a short English instruction ("Turn right onto Main St","Arrive at destination", etc.).src/routing.test.ts— Existing Valhalla tests retained against the extracted client; new tests cover OSRM URL construction, response parsing, error paths, geometry concatenation, maneuver-type mapping, and instruction synthesis.Test plan
npm run type-checkcleannpm run lintcleannpm test— 145 / 145 pass (64 inrouting.test.ts)npm run buildsucceeds; gzipped bundle 104.20 kB (well under the 105 kB cap)ROUTING_PROVIDER = 'osrm', runnpm run dev, drop a pin and tap "Navigate here" — verify route polyline draws, maneuver icons appear, ETA updates, and arrival fires.ROUTING_PROVIDER = 'valhalla'is unchanged.Assumptions
maneuver.typevalues map cleanly to a Valhalla numeric type subset; unknown maneuvers map to0(default arrow icon) and synthesise a generic "Continue" instruction.routed-{car,bike,foot}/route/v1/{driving,cycling,walking}/...) is what's currently deployed atrouting.openstreetmap.de. If a single-host layout is preferred,osrmBackend()is the only point of change.Closes #189